<template>
  <div id="articalDetail" v-loading="store.loading">
    <div class="back">
      <div class="back-content" @click="$router.back()">
        <i class="el-icon-arrow-left"></i>
        <span>返回</span>
      </div>
    </div>
    <div class="artical-detail-content" v-if="detail && !store.loading">
      <div class="detail-title">{{ detail.title }}</div>
      <div class="detail-sub-title">
        <div class="author">
          <div class="avatar"><img :src="detail.user.avatar" alt="" /></div>
          <div class="user-info">
            <div class="name">{{ detail.user.name }}</div>
            <div class="create-time">
              {{ timestampToDate(detail.create_time).substring(0, 10) }}
            </div>
          </div>
        </div>
        <div class="op-list">
          <el-button type="warning" size="mini" @click="handleLikeClick">
            <i class="el-icon-caret-top"></i>点赞 （{{ detail.like }}）
          </el-button>
          <el-button type="warning" size="mini" @click="handleHateClick">
            <i class="el-icon-caret-bottom"></i>
          </el-button>
        </div>
      </div>
      <div class="detail-main" v-html="detail.description"></div>

      <!-- <div class="artical-op">
        <div class="artical-op-artical prev-artical"><i class="el-icon-arrow-left"></i><span>上一章</span></div>
        <div class="artical-op-artical next-artical"><span>下一章</span><i class="el-icon-arrow-right"></i></div>
      </div> -->
    </div>
  </div>
</template>

<script setup lang="ts">
import { marked } from 'marked'
import { Ref, ref } from 'vue'
import { useMainStore } from '/@/store'
import { useRoute } from 'vue-router'
import { getBlogDetail, setBlogLike } from '/@/api/api'
import { timestampToDate } from '/@/utils/date'

const route = useRoute()
const store = useMainStore()
const detail = ref(null)

// 获取详情
const getDetail = async () => {
  store.setLoading(true)
  const res: any = await getBlogDetail({
    uuid: route.params.id
  }).finally(() => {
    store.setLoading(false)
  })

  if (res.code === 1) {
    detail.value = res.data
    if (detail.value) {
      detail.value.description = marked(detail.value.description, {
        sanitize: true
      })
    }
  }
}
getDetail()

// 设置喜欢
const handleLikeClick = async () => {
  const res: any = await setBlogLike({ uuid: detail.value.uu_id })
  if (res.code === 1) {
    detail.value.like = parseInt(detail.value.like) + 1
  }
}

// 设置不喜欢
const handleHateClick = () => {
  // this.$message.error('您忍心嘛！！')
}
</script>

<style lang="less" scoped>
#articalDetail {
  .artical-detail-content {
    padding: 30px;

    .detail-title {
      font-weight: bold;
      font-size: 20px;
    }

    .detail-sub-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-top: 25px;

      .author {
        display: flex;
        align-items: center;

        .avatar {
          width: 50px;
          height: 50px;

          img {
            width: 100%;
            height: 100%;
          }
        }

        .user-info {
          margin-left: 20px;
          font-size: 13px;

          .name {
          }

          .create-time {
            margin-top: 10px;
          }
        }
      }
    }

    .detail-main {
      min-height: 400px;
      margin-top: 35px;
      font-family: Menlo;
      line-height: 1.5;
    }
  }

  .detail-cover {
    margin-top: 30px;

    img {
      width: 100%;
      height: auto;
    }
  }

  .artical-op {
    display: flex;
    justify-content: space-between;
    margin-top: 60px;

    .artical-op-artical {
      display: flex;
      align-items: center;

      span {
        font-size: 14px;
        margin: 0 10px;
      }
    }
  }
}
</style>
